package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

@Service
public class SettingManager {

    @Reference
    private NotificationService notificationService;

    @Reference
    private QuestionService questionService;

    @Reference
    private BlackListService blackListService;


    public SettingVo findUserSetting() {
        User user = UserHolder.get();

        Notification notification = notificationService.findByUserId(user.getId());
        Question question = questionService.findByUserId(user.getId());

        SettingVo settingVo = new SettingVo();
        settingVo.setId(user.getId());
        settingVo.setPhone(user.getPhone());

        if (notification != null) {
            BeanUtil.copyProperties(notification,settingVo);
        }

        if (question == null) {
            settingVo.setStrangerQuestion("这个小友很懒");
        } else {
            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }

        return settingVo;
    }

    //保存或者更新陌生人问题
    public void saveOrUpdateQuestions(String content) {
        Long id = UserHolder.get().getId();
        Question question = questionService.findByUserId(id);
        if (question != null) {
            question.setStrangerQuestion(content);
            questionService.update(question);
        } else {
            question = new Question();
            question.setUserId(id);
            question.setStrangerQuestion(content);
            questionService.save(question);
        }
    }

    //保存或者更新推送设置
    public void saveOrUpdateNotifications(Notification notificationParam) {
        Long id = UserHolder.get().getId();

        Notification notification = notificationService.findByUserId(id);

        if (notification != null) {
            notificationParam.setId(notification.getId());
            notificationService.update(notificationParam);
        } else {
            notificationParam.setUserId(id);
            notificationService.save(notificationParam);
        }
    }

    //黑名单列表查询
    public PageBeanVo findBlackList(Integer pageNum, Integer pageSize) {
        Long userId = UserHolder.get().getId();

        return blackListService.findBlackUserByUserId(userId, pageNum, pageSize);
    }

    //删除黑名单用户
    public void deleteByBlackUserId(Integer blackUserId) {
        Long userId = UserHolder.get().getId();

        blackListService.deleteByBlackUserId(userId,blackUserId);
    }
}
